package net.oschina.bilbodai.common.beanutil.property.asm.lexer.code;

import java.util.List;

/**
 * @author daimao  Date: 16/4/22 Time: 上午11:35
 * @version $Id$
 */
public class CodeApply {

    private final Class<?> stackType; //上次token执行完,留在栈上的类型
    private final Class<?> parentType;
    private final Class<?> thisType; //thisType maybe modified by MapApplier when special extended type key set ex: <string>name
    private final List<Class<?>> thisGenericTypes;
    private final String thisPropertyName;
    private final String mapKey; // "id"
    private final String mapKeyType; //<string>
    private final int index; //array[index] or list[index]

    public CodeApply(Class<?> stackType, Class<?> parentType, Class<?> thisType, List<Class<?>> thisGenericTypes, String thisPropertyName, String mapKey, String mapKeyType, int index) {
        this.stackType = stackType;
        this.parentType = parentType;
        this.thisType = thisType;
        this.thisGenericTypes = thisGenericTypes;
        this.thisPropertyName = thisPropertyName;
        this.mapKey = mapKey;
        this.mapKeyType = mapKeyType;
        this.index = index;
    }

    private CodeApply(Builder builder) {
        stackType = builder.stackType;
        parentType = builder.parentType;
        thisType = builder.thisType;
        thisGenericTypes = builder.thisGenericTypes;
        thisPropertyName = builder.thisPropertyName;
        mapKey = builder.mapKey;
        mapKeyType = builder.mapKeyType;
        index = builder.index;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static Builder newBuilder(CodeApply copy) {
        Builder builder = new Builder();
        builder.stackType = copy.stackType;
        builder.parentType = copy.parentType;
        builder.thisType = copy.thisType;
        builder.thisGenericTypes = copy.thisGenericTypes;
        builder.thisPropertyName = copy.thisPropertyName;
        builder.mapKey = copy.mapKey;
        builder.mapKeyType = copy.mapKeyType;
        builder.index = copy.index;
        return builder;
    }

    public static final class Builder {
        private Class<?> stackType;
        private Class<?> parentType;
        private Class<?> thisType;
        private List<Class<?>> thisGenericTypes;
        private String thisPropertyName;
        private String mapKey;
        private String mapKeyType;
        private int index;

        private Builder() {
        }

        public Builder withStackType(Class<?> val) {
            stackType = val;
            return this;
        }

        public Builder withParentType(Class<?> val) {
            parentType = val;
            return this;
        }

        public Builder withThisType(Class<?> val) {
            thisType = val;
            return this;
        }

        public Builder withThisGenericTypes(List<Class<?>> val) {
            thisGenericTypes = val;
            return this;
        }

        public Builder withThisPropertyName(String val) {
            thisPropertyName = val;
            return this;
        }

        public Builder withMapKey(String val) {
            mapKey = val;
            return this;
        }

        public Builder withMapKeyType(String val) {
            mapKeyType = val;
            return this;
        }

        public Builder withIndex(int val) {
            index = val;
            return this;
        }

        public CodeApply build() {
            return new CodeApply(this);
        }
    }

    public Class<?> getStackType() {
        return stackType;
    }

    public Class<?> getParentType() {
        return parentType;
    }

    public Class<?> getThisType() {
        return thisType;
    }

    public List<Class<?>> getThisGenericTypes() {
        return thisGenericTypes;
    }

    public String getThisPropertyName() {
        return thisPropertyName;
    }

    public String getMapKey() {
        return mapKey;
    }

    public String getMapKeyType() {
        return mapKeyType;
    }

    public int getIndex() {
        return index;
    }
}